package com.dvd.djk.gdjk001;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.*;

import com.dvd.common.CommonAction;
import com.dvd.dao.dvd.TDvd;

public class GDJK001Action extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonAction.New(request,this.servlet);
		/*get Parameters and set default values to them*/
		int pageId;
		try
		{	pageId =Integer.parseInt(request.getParameter("pageId"));
		}catch(Exception e)
		{
			pageId = 1;
		}
		String action = request.getParameter("action");
		if(action == null)
		{
			action = "paging";
		}
		
		/*according action,dispatch requests to relevant methods*/
		if(action.equals("paging"))
		{
			return this.paging(pageId, mapping, form, request, response);
		}
		else
		{
			assert("Invalid Action" == null);
			return null;
		}
	}
	private final int PER_PAGE_COUNT = 15;
	
	public ActionForward paging(int pageId,ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		
		GDJK001Model model = new GDJK001Model();
		List<TDvd> dvds = model.searchDvdList(pageId, this.PER_PAGE_COUNT);
		request.setAttribute("List_TDvd", dvds);
		ActionForward forward = new ActionForward(mapping.findForward("GDJK001").getPath() + 
								"?pageId=" + pageId + "&pageCount=" + model.getPageCount(this.PER_PAGE_COUNT));
		return forward;
	}

}
